<script setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import AddEditModal from './AddEditModal.vue'
import DetailedModal from './DetailedModal.vue'

import { colChange, currentHook, handleAdd, handleSelectionChange } from './current-hook'
import Pagination from '@/components/Pagination/index.vue'
import ColumnFilter from '@/components/ColumnFilter.vue'
import RightToolBar from '@/components/RightToolBar.vue'
import { getUserList } from '@/api/system/admin'
import { i18n } from '@/lang'
const { t } = i18n.global

/*查询模块*/
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  deptId: '',
  userName: '', //用户名称
  phonenumber: '', //手机号码
  status: '', //状态
  beginTime: '', //创建时间开始时间
  endTime: '', //创建时间结束时间
  dateRange: [] //创建时间
})

// 重新查询
const handleQuery = () => {
  queryParams.pageNum = 1
  getList(queryParams)
}
// 重置查询
const resetQuery = () => {
  resetData(queryParams, bakQueryParams)
  handleQuery()
}

//编辑页
const handleUpdate = (row) => {
  refAddEditModal.value.showModal({ userId: row.id })
}

//详情页
const handleClick = (row) => {
  refDetailedModal.value.showModal({ userId: row.id })
}

// 获取用户列表
const getList = () => {
  loading.value = true
  const skip = (queryParams.pageNum - 1) * queryParams.pageSize
  getUserList({
    page: skip,
    size: queryParams.pageSize
  }).then((res) => {
    loading.value = false
    if (!res.data) return
    userList.value = res.data
    totalNum.value = res.total
  })
}

//重置密码
const handleResetPwd = (row) => {
  ElMessageBox.prompt(`请输入"${row.userName}"的新密码`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    closeOnClickModal: false,
    inputPattern: /^.{5,20}$/,
    inputErrorMessage: '用户密码长度必须介于 5 和 20 之间'
  }).then(({ value }) => {
    ElMessage({ message: `修改成功，新密码是：${value}`, type: 'success' })
  })
}

const {
  refAddEditModal,
  refDetailedModal,
  refElTable,
  totalNum,
  loading,
  userList,
  showSearch,
  tableHeadColumns,
  handleDelete
} = currentHook(queryParams, getList)

onMounted(() => handleQuery())
</script>

<template>
  <div class="p-10px">
    <!-- <el-form v-show="showSearch" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model.trim="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          class="wi-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model.trim="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          class="wi-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建时间" style="width: 240px}">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">{{ t('user.create') }}</el-button>
      </el-col>
      <RightToolBar v-model:showSearch="showSearch" @queryTable="getList" />
      <ColumnFilter v-if="userList.length" :is-operation="true" :cols="tableHeadColumns" @colChange="colChange" />
    </el-row>
    <el-table
      ref="refElTable"
      v-loading="loading"
      height="calc(100vh - 370px)"
      border
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <!--column头字段-->
      <template v-for="item in tableHeadColumns">
        <el-table-column
          v-if="!item.isTemplate && item.showColumn"
          :key="item.prop"
          show-overflow-tooltip
          v-bind="item"
          :align="item.align || 'center'"
          :prop="item.prop"
          :label="t(item.label)"
        />
      </template>

      <el-table-column :label="t('user.action')" align="center" width="150" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <div v-if="row.userName !== 'admin'">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" size="large" @click="handleUpdate(row)" />
            </el-tooltip>

            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="CircleCheck" size="large" @click="handleClick(row)" />
            </el-tooltip>

            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" size="large" @click="handleDelete(row)" />
            </el-tooltip>
            <!-- <el-tooltip  content="重置密码" placement="top">
              <el-button link type="primary" icon="Key" size="large" @click="handleResetPwd(row)" />
            </el-tooltip> -->
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="columnSE">
      <Pagination
        v-show="totalNum > queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        :total="totalNum"
        @pagination="getList"
      />
    </div>
    <AddEditModal ref="refAddEditModal" />
    <DetailedModal ref="refDetailedModal" />
  </div>
</template>

<style scoped></style>
